"""获取网页信息模块
从12306获取用户想要的必要列车信息文件读取到本地
"""
import json
import urllib.request
import ssl


ssl._create_default_https_context = ssl._create_unverified_context


def city_find():
    city_list = {}
    with open('./支持文件/city.txt', 'r', encoding='utf8') as ci:
        # print(ci.read())
        for x in [i.strip().split(':') for i in ci]:
            city_list[x[0]] = x[1]
    return city_list

city = city_find()


def getlist(date) -> object:
    # date = input('你要查询的日期(格式:1111-22-33):')
    try:
        from_station = city[input('请输入出发站:')]
        to_station = city[input('请输入到达站:')]
        print(f'出发站代码:{from_station}\t到达站代码{to_station}')
    except:
        print('这个城市没有找到,请重新输入:')
        getlist(date_now)
    data0 = urllib.request.urlopen(f"https://kyfw.12306.cn/otn/leftTicket/query?leftTicketDTO.train_date={date}&leftTicketDTO.from_station={from_station}&leftTicketDTO.to_station={to_station}&purpose_codes=ADULT", ).read()
    data1 = json.loads(data0)
    return data1['data']


date_now = input('你要查询的日期(格式:1111-22-33):')
data_ = getlist(date_now)
print(data_)
